﻿namespace DslEditorPowerToy.Controls.Languages
{
    using Microsoft.VisualStudio.Shell.Interop;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel.Design;
    using System.Runtime.CompilerServices;

    public class SelectionContainerSelectionService : ISelectionService
    {
        private ISelectionContainer _selectionContainer;

        public event EventHandler SelectionChanged;

        public event EventHandler SelectionChanging;

        [CLSCompliant(false)]
        public SelectionContainerSelectionService(ISelectionContainer container)
        {
            this._selectionContainer = container;
        }

        public bool GetComponentSelected(object component)
        {
            ICollection selectedComponents = this.GetSelectedComponents();
            if (selectedComponents != null)
            {
                ArrayList list = new ArrayList(selectedComponents);
                foreach (object obj2 in list)
                {
                    if (component == obj2)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public ICollection GetSelectedComponents()
        {
            uint pc = 0;
            if ((this._selectionContainer.CountObjects(2, out pc) == 0) && (pc > 0))
            {
                object[] apUnkObjects = new object[pc];
                if ((this._selectionContainer.GetObjects(2, pc, apUnkObjects) == 0) && (apUnkObjects != null))
                {
                    return new List<object>(apUnkObjects);
                }
            }
            return null;
        }

        protected void OnSelectionChanged()
        {
            if (this.SelectionChanged != null)
            {
                this.SelectionChanged(this, new EventArgs());
            }
        }

        protected void OnSelectionChanging()
        {
            if (this.SelectionChanging != null)
            {
                this.SelectionChanging(this, new EventArgs());
            }
        }

        public void SetSelectedComponents(ICollection selectedComponents)
        {
        }

        public void SetSelectedComponents(ICollection selectedComponents, SelectionTypes selectionType)
        {
            throw new NotImplementedException();
        }

        public object PrimarySelection
        {
            get
            {
                ICollection selectedComponents = this.GetSelectedComponents();
                if ((selectedComponents != null) && (selectedComponents.Count > 0))
                {
                    return new ArrayList(selectedComponents)[0];
                }
                return null;
            }
        }

        public int SelectionCount
        {
            get
            {
                uint pc = 0;
                if (this._selectionContainer.CountObjects(2, out pc) == 0)
                {
                    return (int) pc;
                }
                return 0;
            }
        }
    }
}

